package com.hzy.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzy.cms.domain.AreaInfo;
import com.hzy.cms.domain.bo.AreaInfoBo;
import com.hzy.cms.domain.vo.AreaInfoVo;
import com.hzy.cms.mapper.AreaInfoMapper;
import com.hzy.cms.service.IAreaInfoService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 中国地区Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-13
 */
@RequiredArgsConstructor
@Service
public class AreaInfoServiceImpl implements IAreaInfoService {

    private final AreaInfoMapper baseMapper;

    /**
     * 查询中国地区
     */
    @Override
    public AreaInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询中国地区列表
     */
    @Override
    public List<AreaInfoVo> queryList(AreaInfoBo bo) {
        LambdaQueryWrapper<AreaInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AreaInfo> buildQueryWrapper(AreaInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AreaInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, AreaInfo::getParentId, bo.getParentId());
        lqw.like(StringUtils.isNotBlank(bo.getShortname()), AreaInfo::getShortname, bo.getShortname());
        lqw.like(StringUtils.isNotBlank(bo.getName()), AreaInfo::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getMergename()), AreaInfo::getMergename, bo.getMergename());
        lqw.eq(bo.getLevel() != null, AreaInfo::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getPinyin()), AreaInfo::getPinyin, bo.getPinyin());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), AreaInfo::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getZip()), AreaInfo::getZip, bo.getZip());
        lqw.eq(StringUtils.isNotBlank(bo.getFirst()), AreaInfo::getFirst, bo.getFirst());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), AreaInfo::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), AreaInfo::getLat, bo.getLat());
        return lqw;
    }

    /**
     * 新增中国地区
     */
    @Override
    public Boolean insertByBo(AreaInfoBo bo) {
        AreaInfo add = BeanUtil.toBean(bo, AreaInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改中国地区
     */
    @Override
    public Boolean updateByBo(AreaInfoBo bo) {
        AreaInfo update = BeanUtil.toBean(bo, AreaInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AreaInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除中国地区
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<AreaInfoVo> queryPageList(AreaInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AreaInfo> lqw = buildQueryWrapper(bo);
        Page<AreaInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
